package cnpm.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;

import cnpm.dao.DepartmentDAO;
import cnpm.entity.Department;
import cnpm.util.HibernateUtil;

public class DepartmentDAOImpl implements DepartmentDAO{

	private SessionFactory sessionFactory;
	private Session session;
	
	public DepartmentDAOImpl() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getAllDepartments() {
		List<Department> list=null;
		try{
			session = sessionFactory.openSession();
			list = (List<Department>) session.createCriteria(Department.class)
					.addOrder(Order.asc("deptName")).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}

	@Override
	public Department getDepartmentByID(int deptID) {
		Department d =null;
		try{
			session = sessionFactory.openSession();
			d = (Department) session.get(Department.class, deptID);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		return d;
	}

	
}
